---
# collectd client setup

# install pkg
- name: install collectd
  package: name=collectd state=present
  tags:
  - collectd
  when: ansible_distribution_major_version|int < 22

# install pkg
- name: install collectd
  dnf: name=collectd state=present
  tags:
  - collectd
  when: ansible_distribution_major_version|int > 21

# install collectd-disk on F25+ (it was split out)
- name: install collectd-disk
  dnf: name=collectd-disk state=present
  tags:
  - collectd
  when: ansible_distribution_major_version|int > 24

# install collected.conf
- name: /etc/collectd.conf
  template: src=collectd.conf.j2 dest=/etc/collectd.conf
  tags:
  - collectd
  notify:
  - restart collectd

# install collectd-network config
- name: /etc/collectd.d/network.conf
  template: src=network-client.conf.j2 dest=/etc/collectd.d/network.conf
  tags:
  - collectd
  notify:
  - restart collectd
  when: not inventory_hostname.startswith('log')

# install collectd-network config
- name: /etc/collectd.d/network.conf
  copy: src=network-server.conf dest=/etc/collectd.d/network.conf
  tags:
  - collectd
  notify:
  - restart collectd
  when: inventory_hostname.startswith('log')

# apache - localhost only - pretty much any apache server
- name: install collectd-apache (yum)
  package: state=present name=collectd-apache
  tags:
  - collectd
  notify:
  - restart collectd
  when: collectd_apache and ansible_distribution_major_version|int < 22

- name: install collectd-apache (dnf)
  dnf: state=present name=collectd-apache
  tags:
  - collectd
  notify:
  - restart collectd
  when: collectd_apache and ansible_distribution_major_version|int > 21

- name: /etc/collectd/apache.conf
  copy: src=apache.conf dest=/etc/collectd.d/apache.conf
  tags:
  - collectd
  notify:
  - restart collectd
  when: collectd_apache

- name: Install libsemanage-python so we can set an sebool below
  package: name=libsemanage-python state=present
  tags:
  - collectd
  when: collectd_apache is defined and ansible_distribution_major_version|int < 22

- name: Let collectd talk to things over tcp
  seboolean: name=collectd_tcp_network_connect state=yes persistent=yes
  tags:
  - collectd
  ignore_errors: True
  notify:
  - restart collectd
  when: ( collectd_apache is defined ) and ansible_selinux.status != "disabled"

- name: enable collectd nfs module
  copy: src=nfs.conf dest=/etc/collectd.d/nfs.conf
  tags:
  - collectd
  notify:
  - restart collectd

# Three tasks for handling our (two) custom selinux modules.
- name: ensure a directory exists for our custom selinux module
  file: dest=/usr/share/collectd state=directory
  tags:
  - collectd
  - selinux

- name: copy over our general collectd selinux module
  copy: src=selinux/fi-collectd.pp dest=/usr/share/collectd/fi-collectd.pp
  register: ficgeneral_module
  tags:
  - collectd
  - selinux

- name: check to see if its even installed yet
  shell: semodule -l | grep fi-collectd | wc -l
  register: ficgeneral_grep
  check_mode: no
  changed_when: "'0' in ficgeneral_grep.stdout"
  tags:
  - collectd
  - selinux

- name: install our general collectd selinux module
  command: semodule -i /usr/share/collectd/fi-collectd.pp
  when: ficgeneral_module is changed or ficgeneral_grep is changed
  tags:
  - collectd
  - selinux

- name: copy over our pstorefs/collectd selinux module (rhel6 has no pstorefs)
  copy: src=selinux/fi-pstorefs.pp dest=/usr/share/collectd/fi-pstorefs.pp
  when: ansible_distribution_major_version|int != 6
  register: ficpstorefs_module
  tags:
  - collectd
  - selinux

- name: check to see if its even installed yet
  shell: semodule -l | grep fi-pstorefs | wc -l
  when: ansible_distribution_major_version|int != 6
  register: ficpstorefs_grep
  check_mode: no
  changed_when: "'0' in ficpstorefs_grep.stdout"
  tags:
  - collectd
  - selinux

- name: install our pstorefs/collectd selinux module
  command: semodule -i /usr/share/collectd/fi-pstorefs.pp
  when: ansible_distribution_major_version|int != 6 and (ficpstorefs_module is changed or ficpstorefs_grep is changed)
  tags:
  - collectd
  - selinux



# each of the below should move to a separate task list
# since they are odd-balls and one-offs

# fedmsg - busgateway## only
  # add /usr/share/collectd/fedmsg-types.db

# memcached - memcached only

# postgres - this is a conn check
## add /usr/share/collectd/pgconn-types.db

# openvpn - for bastion/openvpn gateways only

# mysql
## collectd-mysql

# haproxy
## add /usr/share/collectd/haproxy-types.db
## add socat pkg
##

# webproxy

# enable collectd
- name: enable collectd svc
  service: state=started enabled=true name=collectd
  tags:
  - collectd
